package com.zcj.demo.user.service;

import com.zcj.demo.user.domain.UserDO;
import com.zcj.demo.user.repository.UserRepository;
import com.zcj.demo.user.service.dto.UserDTO;
import com.zcj.demo.user.service.mapper.UserMapper;
import com.zcj.demo.utils.specification.Specifications;
import com.zcj.demo.web.user.vo.UserVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * User service层
 *
 * @author 自动生成
 * @date 2020-10-13 16:02:43
 */
@Service
public class UserService {

    private final UserRepository userRepository;
    private final UserMapper userMapper;

    public UserService(UserMapper userMapper,
                       UserRepository userRepository) {
        this.userMapper = userMapper;
        this.userRepository = userRepository;
    }

    /**
     * 分页查询
     *
     * @param pageable 分页信息
     * @param userVO   查询条件
     * @return 分页列表
     */
    public Page<UserDTO> findByPage(Pageable pageable, UserVO userVO) {
        Specification<UserDO> spec = this.getAllSpecification(userVO);
        return userRepository.findAll(spec, pageable).map(userMapper::entityToDTO);
    }

    /**
     * 条件查询所有
     *
     * @param userVO 查询条件
     * @return 所有符合条件的列表
     */
    public List<UserDTO> findAll(UserVO userVO) {
        Specification<UserDO> spec = this.getAllSpecification(userVO);
        return userRepository.findAll(spec).stream().map(userMapper::entityToDTO).collect(Collectors.toList());
    }

    /**
     * 获取所有Specification
     *
     * @param userVO 查询条件
     * @return specification
     */
    private Specification<UserDO> getAllSpecification(UserVO userVO) {
        return Specifications.<UserDO>and()
                .like("userName", userVO.getUsername())
                .like("password", userVO.getPassword())
                .like("name", userVO.getName())
                .like("avatar", userVO.getAvatar())
                .like("email", userVO.getEmail())
                .like("phone", userVO.getPhone())
                .build();
    }

    /**
     * 所有符合条件的列表
     *
     * @param id 编号
     * @return 单条数据
     */
    public UserDTO findById(String id) {
        return userMapper.entityToDTO(userRepository.getOne(id));
    }

    /**
     * 创建
     *
     * @param userDTO 创建的信息
     * @return 创建的实体
     */
    public UserDTO create(UserDTO userDTO) {
        UserDO update = userMapper.dtoToEntity(userDTO, new UserDO());
        return userMapper.entityToDTO(userRepository.saveAndFlush(update));
    }

    /**
     * 更新
     *
     * @param userDTO 更新的信息
     * @return 更新之后的实体
     */
    public UserDTO update(UserDTO userDTO) {
        UserDO original = userRepository.getOne(userDTO.getId());
        UserDO update = userMapper.dtoToEntity(userDTO, original);
        return userMapper.entityToDTO(userRepository.saveAndFlush(update));
    }

    /**
     * 根据编号删除
     *
     * @param id 编号
     */
    public void delete(String id) {
        userRepository.deleteById(id);
    }
}
